<template>
  <div class="app-container" v-loading="pageLoading">
    <el-button type="primary" class="fr" v-permission='"functionConfig.add"' @click="createItem">创建</el-button>
    <div class="filter-container">
      <el-input style="width: 300px" placeholder="输入查询条件按回车" clearable prefix-icon="el-icon-search" class="filter-item"
        @keyup.enter.native="research" @clear="research" v-model="query.queryString"></el-input>
      <el-button class="filter-item" type="success" plain @click="research">查询</el-button>
      <el-button class="filter-item" type="info" plain @click="exportData" :loading="exportLoading">导出 </el-button>
    </div>
    <z-table :list="list" :tableProps="tableProps" :columns="columns" @editItem="editItem" @deleteItem="deleteItem">
    </z-table>
    <z-pagination :pagination="pagination" :total="total" :page.sync="query.page" :limit.sync="query.size"
      @change="getList"></z-pagination>
    <z-form-dialog :name="name" :data="data" :formProps="formProps" :fields="fields" @submit="submit"
      :submitLoading="submitLoading" :visible.sync="editFormVisible"></z-form-dialog>
  </div>
</template>

<script>
import commonMixin from '@/views/_common/mixin'
export default {
  mixins: [commonMixin],
  name: 'functionConfig',
  data: function () {
    return {
      api: this.$api.functionconfig,
      apiEdit: this.$api.functionconfig + 'add',
      name: 'PDA功能页配置',
      columns: [
        { title: '功能页', key: 'page_name', width: 200 },
        { title: '单据类型', key: 'order_type_name' },
        { title: '条码解析规则', key: 'sn_type_name' },
        { title: '出库仓库类型', key: 'warehouse_out_type_name', width: 200 },
        { title: '入库仓库类型', key: 'warehouse_in_type_name' },
        { title: '修改人', key: 'modify_user', width: 100 },
        { title: '最后修改', key: 'modify_time', width: 140 },
      ],
      fields: [
        { title: '功能页', key: 'page', required: true, name: 'select', props: { filterable: true } },
        { title: '单据类型', key: 'order_type', name: 'select', props: { multiple: true } },
        { title: '条码解析规则', key: 'sn_split_rule', name: 'select', options: [], props: { multiple: true } },
        { title: '出库仓库类型', key: 'warehouse_out_type', name: 'select', options: [], props: { multiple: true } },
        { title: '入库仓库类型', key: 'warehouse_in_type', name: 'select', options: [], props: { multiple: true } },
        {
          title: '开启条码唯一性',
          key: 'is_onlysncheck',
          name: 'switch',
          props: { inactiveValue: 'N', activeValue: 'Y' },
        },
        { title: '备注', key: 'remarks', name: 'textarea' },
      ],
      typeOptions: [
        // { label: '良品仓', value: '1' },
        // { label: '报废仓', value: '2' },
        // { label: '不良品仓', value: '3' },
      ],
      orderTypeOptions: [],
      snTypeOptions: [],
      pageOptions: [],
      initData: {
        is_onlysncheck: 'N',
      },
    }
  },
  methods: {
    formatList(data) {
      _.forEach(data, (v) => {
        if (v.warehouse_out_type)
          v.warehouse_out_type_name = this.joinString(v.warehouse_out_type, this.typeOptions, 'value', ['label'])
        if (v.warehouse_in_type)
          v.warehouse_in_type_name = this.joinString(v.warehouse_in_type, this.typeOptions, 'value', ['label'])
        if (v.order_type)
          v.order_type_name = this.joinString(v.order_type, this.orderTypeOptions, 'id', ['code', 'name'])
        if (v.sn_split_rule) v.sn_type_name = this.joinString(v.sn_split_rule, this.snTypeOptions, 'id', ['name'])
        if (v.page) v.page_name = this.joinString(v.page, this.pageOptions, 'id', ['menu_name'])
      })
      return data
    },
    joinString(ary, list, field, labelArray) {
      if (!ary) ary = []
      if (!Array.isArray(ary)) ary = ary.split(',')
      let arraytmp = []
      _.forEach(ary, (a) => {
        let obj = _.find(list, function (o) {
          return a == o[field]
        })
        if (!obj) return ''
        let label = ''
        _.forEach(labelArray, (l) => {
          label += obj[l] + ' '
        })
        label = label.trim()
        if (obj) arraytmp.push(label)
      })
      return arraytmp.join(';')
    },
  },
  created: async function () {
    this.$request(this.$api.dictionary + 'getdirectchildren', {
      parentType: 'warehouse_type',
    }).then((r) => {
      this.typeOptions = r
    })
    await this.$request(this.$api.ordertype + 'getlist', { page: 1, size: 999 }).then((r) => {
      this.setFieldOptions('order_type', r.list, 'id', ['code', 'name'])
      this.orderTypeOptions = r.list
    })
    await this.$request(this.$api.snsplitrule + 'getlist', { page: 1, size: 999 }).then((r) => {
      this.setFieldOptions('sn_split_rule', r.list, 'id', ['name'])
      this.snTypeOptions = r.list
    })
    await this.$request(this.$api.menu + 'getlist', { type: 'PDA', page: 1, size: 999 }).then((r) => {
      this.setFieldOptions('page', r, 'id', 'menu_name')
      this.pageOptions = r
    })
    this.getFieldConfig('warehouse_in_type').options = this.typeOptions
    this.getFieldConfig('warehouse_out_type').options = this.typeOptions
    this.getList()
  },
}
</script>
<style scoped>
</style>
